<template>
  <!--基本信息  -->
  <div class="wrapper" style="width:500px; margin:0 auto">
    <FormPanel
      ref="formPanel"
      :list="initForm"
      :cols="1"
      :formType="type"
      :labelWidth="110"
      :isSubmitBtn="false"
      @formChange="formChangeHandle"
    />
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right'
      }"
    >
      <el-button @click="closeDialog">取 消</el-button>
      <el-button
        v-if="type !== 'show'"
        :loading="saveLoading"
        @click="saveDialog"
        type="primary"
      >{{ btnText }}</el-button>
    </div>
  </div>
</template>

<script>
import { authority } from '@/utils/authMixin';
import { dictionary } from '@/utils/dictMixin';
import { addMarketType, updateMarketType, majorClass } from '@/api/cms-api/projectOperating/definition/majorClass/index';
export default {
  mixins: [authority, dictionary],
  props: {
    type: {
      type: String,
      default: 'add'
    },
    rowData: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    this.conf = { add: '保 存', edit: '修 改' };
    this.saveData = null;
    return {
      initForm: this.initFormFun(),
      saveLoading: false,
      formValue: {}
    };
  },
  computed: {
    btnText() {
      return this.conf[this.type];
    }
  },
  created() {
    this.init();
    //初始化下拉列表
    this.getParentType();
  },
  methods: {
    //获取事业部下拉列表数据
    async getParentType() {
      const res = await majorClass();
      if (res.resultCode === 200) {
        this.initForm[0].itemList = res.data.map(x => ({ value: x.typeId, text: x.typeName }));
      }
    },

    init() {
      if (this.type === 'edit') {
        // 更新接口需要的记录 ID
        this.recordId = this.rowData.typeId;
        this.initForm.forEach(x => {
          x.initialValue = this.rowData[x.fieldName];
        });
      }
      if (this.type === 'add') {
        this.initForm[3].initialValue = 10031001;
      }
    },
    //搜索数据
    initFormFun() {
      return [
        {
          type: 'SELECT',
          label: '活动大类名称:',
          fieldName: 'parentTypeId',
          placeholder: '请选择',
          disabled: this.type == 'edit',
          initialValue: '',
          itemList: [],
          rules: [{ required: true, message: '请选择活动大类名称', trigger: 'blur' }]
        },
        {
          type: 'INPUT',
          label: '活动小类代码:',
          fieldName: 'typeCode',
          disabled: this.type == 'edit',
          rules: [{ required: true, message: '请输入活动小类代码', trigger: 'blur' }, { max: 20, message: '活动小类代码长度不能超过20', trigger: 'blur' },{pattern:/^[A-Za-z0-9]+$/,message:'只能填写字母和数字',trigger: 'blur'}]
        },
        {
          type: 'INPUT',
          label: '活动小类名称:',
          fieldName: 'typeName',
          disabled: this.type == 'edit',
          rules: [{ required: true, message: '请输入活动小类名称', trigger: 'blur' }, { max: 20, message: '活动小类名称长度不能超过20', trigger: 'blur' }]
        },
        {
          type: 'SELECT',
          label: '状态:',
          fieldName: 'status',
          placeholder: '请选择',
          initialValue: '',
          itemList: this.createDictList('1003'),
          rules: [{ required: true, message: '请选择状态', trigger: 'blur' }]
        }
      ];
    },
    async formChangeHandle(val) {
      this.formValue = val;
      if (this.formValue !== null) {
        this.saveLoading = true;
        let res = {};
        if (this.type === 'add') {
          res = await addMarketType(this.formValue);
        }
        if (this.type === 'edit') {
          res = await updateMarketType(this.formValue, this.recordId);
        }
        if (res.resultCode === 200) {
          this.$notify.success({ title: '提示信息', message: '提交成功!' });
          this.closeDialog('refresh');
        } else {
          this.$notify.warning({ title: '提示信息', message: res.errMsg });
          this.saveLoading = false;
        }
      }
    },
    async saveDialog() {
      this.$refs.formPanel.SUBMIT_FORM();
    },
    closeDialog(param) {
      this.$emit('close', false, param);
    }
  }
};
</script>
<style scoped>
.wrapper {
  height: calc(100vh - 110px);
}
</style>
